package com.googlecode.gwt.test.plugin.editors;


import java.io.IOException;
import java.io.StringReader;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
import org.eclipse.ui.texteditor.ContentAssistAction;
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;

import com.googlecode.gwt.test.plugin.resourcesListner.JavaCustomKeyWordsAnalyser;
import com.googlecode.gwt.test.plugin.resourcesListner.MacroChangeListner;

public class GWTCsvEditor extends AbstractDecoratedTextEditor {

	IProject currentProject;

	private ColorManager colorManager;

	private KeyWords keyWords;

	private GWTCsvConfiguration conf;

	public GWTCsvEditor() {
		super();
		colorManager = new ColorManager();
	}

	public void dispose() {
		colorManager.dispose();
		super.dispose();

		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		workspace.removeResourceChangeListener(macroChangeListner);
		workspace.removeResourceChangeListener(javaCustomKeyWordsAnalyser);
	}

	@Override
	protected void createActions() {
		super.createActions();
		ResourceBundle resourceBundle = null;
		try {
			resourceBundle = new PropertyResourceBundle(
					new StringReader(
							"ContentAssistProposal.label=Content assist\nContentAssistProposal.tooltip=Content assist\nContentAssistProposal.description=Provides Content Assistance"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		ContentAssistAction action = new ContentAssistAction(resourceBundle, "contentAssist.", this);
		String id = ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS;
		action.setActionDefinitionId(id);
		setAction("ContentAssist", action);
	}

	@Override
	protected void setDocumentProvider(IEditorInput input) {
		super.setDocumentProvider(input);
		String resourcePath = input.getToolTipText();
		IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(resourcePath);
		if (res != null) {
			this.currentProject = res.getProject();
			this.keyWords = new KeyWords(this.currentProject);
			conf = getGWTCsvConfiguration(colorManager, keyWords, this);
			setSourceViewerConfiguration(conf);
			loadKeyWords();
			conf.getScanner().update();
		} else {
			setSourceViewerConfiguration(new TextSourceViewerConfiguration());
		}
	}

	private GWTCsvConfiguration getGWTCsvConfiguration(ColorManager colorManager2, KeyWords keyWords2, GWTCsvEditor csvEditor) {
		if (conf == null) {
			conf = new GWTCsvConfiguration(colorManager, keyWords, currentProject);
		}
		conf.getScanner().update();
		return conf;
	}

	MacroChangeListner macroChangeListner;
	IResourceChangeListener javaCustomKeyWordsAnalyser;

	private void loadKeyWords() {
		macroChangeListner = new MacroChangeListner(keyWords, currentProject, conf);
		javaCustomKeyWordsAnalyser = new JavaCustomKeyWordsAnalyser(keyWords, currentProject, conf, macroChangeListner);
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		workspace.addResourceChangeListener(macroChangeListner);
		workspace.addResourceChangeListener(javaCustomKeyWordsAnalyser);
	}

}
